<template>
	<view class="page">
		<view class="from_item">
			<view class="label">
				开启连扫
			</view>
			<view class="right">
			 <checkbox-group @change="handleChange">
				  <checkbox :checked="isChecked" value="isChecked" />
			</checkbox-group>			
	</view>
		</view>
		
		
		<view class="from_item">
			<view class="label">条形码</view>
			<view class="right">
				<input v-model="params.spackBarCode" class="uni-input" type="number" focus @input="replaceInput()" />
				<uni-icons class="icon" type="scan" size="30" @click="scanQRCode">
				</uni-icons>
			</view>
		</view>
		<view class="from_item">
			<view class="label">订单号</view>
			<view class="">{{ params.sorderNo }}</view>
		</view>
		<view class="from_item">
			<view class="label">颜色</view>
			<view class="">{{ params.scolorName }}</view>
		</view>
		<view class="from_item">
			<view class="label">尺码</view>
			<view class="">{{ params.ssizeName }}</view>
		</view>
		<view class="from_item">
			<view class="label">任务数</view>
			<view class="">{{ params.taskNum }}</view>
		</view>
		<view class="from_item">
			<view class="label">已报工数</view>
			<view class="">{{ params.sendNum }}</view>
		</view>
		<view class="from_item">
			<view class="label">报工数</view>
			<view class="">
				<input v-model="params.iqty" class="uni-input iqty" type="number" placeholder="请输入"
					@input="handIqtyChange()" />
			</view>
		</view>
		<view class="from_item">
			<view class="label">分配工序</view>
			<view class="seq">
				<uni-data-checkbox multiple v-model="value" :disabled="ischeckbox"  :localdata="sprocedureNameList"
					@change="bindPickerChange">
				</uni-data-checkbox>
			</view>
		</view>
		<view class="from_item">
			<view class="label">报工时间</view>
			<view class="">{{ nowDate }}</view>
		</view>
		<view class="from_item">
			<view class="label">修改时间</view>
			<view class="">{{ nowDate }}</view>
		</view>
		<button class="save" @click="save()" :disabled="btnDisable" size="default">保存</button>
	</view>
</template>

<script>
	import rfidOutput from "@/api/report/rfidOutput"
	import moment from "moment";
	export default {
		data() {
			return {
				isChecked: false,
				ischeckbox: false,
				params: {
					scolorId: "",
					scolorName: "",
					semployeeName: "",
					semployeeNo: "",
					sendNum: "",
					sorderNo: "",
					nprices: "",
					spackBarCode: "",
					sprocedureId: "",
					unSprocedureId: "",
					sprocedureName: "",
					unSprocedureName: "",
					ssizeId: "",
					ssizeName: "",
					sstyleNo: "",
					taskNum: "",
					iqty: "",
					sempty: ""
				},
				sprocedureNameList: [],
				sprocedureIdList: [],
				timer: null,
				index: 0,
				value: [],
				nowDate: "",
				btnDisable: true
			}
		},
		onLoad(query) {
			this.nowDate = moment().format("YYYY-MM-DD HH:mm:ss");
			if (query.spackBarCode) {
				this.getInfo(query.spackBarCode, query.sbillNo);
				this.btnDisable = false;
			}
			uni.hideKeyboard();
		},
		methods: {
			handleChange(e) {
			    this.isChecked = e.detail.value.length > 0;
			},
			getInfo(spackBarCode, sbillNo) {
				uni.showLoading({
					title: '加载中'
				});
				const params = {
					spackBarCode,
					sbillNo
				}
				rfidOutput.queryOutputDataInfo(params).then(res => {
					this.params = Object.assign(this.params, res);
					const sprocedureNameList = this.params.unSprocedureName.split(",");
					const sprocedureIdList = this.params.sprocedureId.split(",");
					const npricesList = this.params.unNprices.split(",");
					const arr = [];
					for (var index = 0; index < sprocedureNameList.length; index++) {
						const obj = {
							text: sprocedureNameList[index],
							value: sprocedureIdList[index],
							prices: npricesList[index] || 0
						}
						arr.push(obj);
					}
					this.sprocedureNameList = arr;
					this.value = sprocedureIdList;
					uni.hideLoading();
				})
			},
			scanQRCode() {
				uni.scanCode({
					success: (res) => {
						this.params.spackBarCode = res.result;
						if (this.timer) {
							clearTimeout(this.timer);
						}
						uni.showLoading({
							title: '加载中'
						});
						this.timer = setTimeout(() => {
							this.getInitData(this.params.spackBarCode);
							this.timer = null;
						}, 1000)
					},
					fail: (err) => {
						uni.showToast({
							title: "无效条码",
							duration: 2000,
							icon: "error"
						});
					}
				});
			},
			replaceInput() {
				if (this.timer) {
					clearTimeout(this.timer);
				}
				uni.showLoading({
					title: '加载中'
				});
				this.timer = setTimeout(() => {
					this.getInitData(this.params.spackBarCode);
					this.timer = null;
				}, 1000)
			},
			bindPickerChange(e) {
				this.params.sprocedureId = e.detail.value.join(",");
				this.params.sprocedureName = e.detail.data.map(item => item.text).join(",");
				this.params.nprices = e.detail.data.map(item => item.prices).join(",");
			},
			handIqtyChange() {

				if (Number(this.params.iqty) +
					Number(this.params.sendNum) >
					Number(this.params.taskNum)) {
					uni.showToast({
						title: "报工数过多",
						duration: 2000,
						icon: "error"
					});
					return false
				}
			},
			getInitData(spackBarCode) {
				// 2501080001
				const params = {
					spackBarCode
				}
				rfidOutput.queryReportInfoByBarCode(params).then(res => {
					if (JSON.stringify(res) === "{}") {
						uni.showToast({
							title: "无数据",
							duration: 2000,
							icon: "error"
						});
						return false
					}
					if (res.sreturnMessage !== "ok") {
						uni.showToast({
							title: res.sreturnMessage,
							duration: 2000,
							icon: "error"
						});
						return false
					} else {
						this.btnDisable = false
					}
					uni.showLoading({
						title: '加载中'
					});
					this.params = Object.assign(this.params, res);
					const sprocedureNameList = this.params.unSprocedureName.split(",");
					const sprocedureIdList = this.params.unSprocedureId.split(",");
					const npricesList = this.params.unNprices.split(",");
					const arr = [];
					for (var index = 0; index < sprocedureNameList.length; index++) {
						const obj = {
							text: sprocedureNameList[index],
							value: sprocedureIdList[index],
							prices: npricesList[index]
						}
						arr.push(obj);
					}
					this.sprocedureNameList = arr;
					// todo 2025/10/11 需要根据条件 来控制是否全选 
					if(this.params.sempty == 'all'){
						this.ischeckbox = true;
						this.value = sprocedureIdList;
						this.params.sprocedureId = this.params.unSprocedureId;
						this.params.sprocedureName = this.params.unSprocedureName;
					}else {
						this.ischeckbox = false;
						this.value = '';
						this.params.sprocedureId = '';
						this.params.sprocedureName = '';
					}
					
					if(this.isChecked){
						this.save();
					}else{
						uni.hideLoading();
					}
				}).catch(error => {
					uni.hideLoading();
				})
			},
			save() {
				uni.showLoading({
					title: '保存中'
				});
				if (Number(this.params.iqty) <= 0) {
					uni.showToast({
						title: "报工数必须大于0",
						duration: 2000,
						icon: "error"
					});
					return
				}
				
				const params = JSON.parse(JSON.stringify(this.params));
				rfidOutput.doReport(params).then(res => {
					// uni.$emit('refreshData');
					// uni.navigateBack();
					
					 // // 获取当前页面路径
					 //  const pages = getCurrentPages()
					 //  const currentRoute = pages[pages.length - 1].route
					  
					 //  // 重载当前页面
					 //  uni.redirectTo({
					 //    url: '/' + currentRoute
					 //  })
					 uni.showToast({title: res ,icon: 'success'});
			
					//刷新数据
					this.refreshData();
					uni.hideLoading();
					
				})
			},
			refreshData(){
				this.params = {
					scolorId: "",
					scolorName: "",
					semployeeName: "",
					semployeeNo: "",
					sendNum: "",
					sorderNo: "",
					nprices: "",
					spackBarCode: "",
					sprocedureId: "",
					unSprocedureId: "",
					sprocedureName: "",
					unSprocedureName: "",
					ssizeId: "",
					ssizeName: "",
					sstyleNo: "",
					taskNum: "",
					iqty: "",
					sempty: ""
				},
				this.sprocedureNameList = [];
				this.sprocedureIdList = [];
				this.timer = null;
				this.index = 0;
				this.value = [];
				this.nowDate =  moment().format("YYYY-MM-DD HH:mm:ss");
				this.btnDisable = true;
			}
		}
	}
</script>

<style scoped>
	page {
		background-color: rgb(242, 242, 242);
		padding-top: 20rpx;
	}

	.page {
		width: 710rpx;
		margin: 0 auto;
		border-radius: 2px;
		background-color: white;
		padding-bottom: 20rpx;
	}

	.from_item {
		min-height: 100rpx;
		padding: 0 20px;
		border-bottom: 1px solid #ddd;
		display: flex;
		align-items: center;
	}

	.label {
		font-size: 16px;
		flex: 1;
		font-weight: bold;
	}

	.right {
		position: relative;
	}

	.right input {
		text-align: right;
		padding-right: 10px;
		position: relative;
		right: 60rpx;
		top: 4px;
		width: 300rpx;
	}

	.iqty {
		text-align: right;
		padding-right: 10px;
		position: relative;
		right: -20rpx;
		top: 4px;
		width: 300rpx;
		font-size: 30rpx;
	}

	.icon {
		position: absolute;
		right: 0;
		top: 0;
	}

	.save {
		margin-top: 10px;
		width: 96%;
		height: 80rpx;
		background: rgb(17, 17, 17);
		border-radius: 10rpx;
		color: white;
		line-height: 80rpx;
		font-size: 24rpx
	}

	.seq {
		max-width: 400rpx;
		float: right;
	}
</style>